package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.StrategyContent;
import cn.wolfcode.trip.base.domain.StrategyDetail;
import cn.wolfcode.trip.base.mapper.StrategyContentMapper;
import cn.wolfcode.trip.base.mapper.StrategyDetailMapper;
import cn.wolfcode.trip.base.query.StrategyDetailQueryObject;
import cn.wolfcode.trip.base.service.IStrategyDetailService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService{

    @Autowired
    StrategyDetailMapper strategyDetailMapper;
    @Autowired
    StrategyContentMapper strategyContentMapper;

    public PageInfo queryForList(StrategyDetailQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());

        return new PageInfo(strategyDetailMapper.queryForList(qo));
    }

    public void update(StrategyDetail detail) {
        //如果是发布状态就设置发布时间
        if (detail.getState()==StrategyDetail.STATE_RELEASE){
                detail.setReleaseTime(new Date());
        }
        strategyDetailMapper.updateByPrimaryKey(detail);
        //保存文章内容
        StrategyContent strategyContent = detail.getStrategyContent();
        //设置关联id
        strategyContent.setId(detail.getId());
        //更新文章内容
        strategyContentMapper.updateByPrimaryKey(strategyContent);
    }

    public void save(StrategyDetail detail) {

        //如果是发布状态就设置发布时间
        if (detail.getState()==StrategyDetail.STATE_RELEASE){
            detail.setReleaseTime(new Date());
        }
        //创建事件
        detail.setCreateTime(new Date());
        strategyDetailMapper.insert(detail);

        //保存文章内容
        StrategyContent strategyContent = detail.getStrategyContent();
        //设置id
        strategyContent.setId(detail.getId());
        //新增文章内容
        strategyContentMapper.insert(strategyContent);
    }
    //查询文章内容
    public StrategyContent getContentById(Long id) {
        return strategyContentMapper.selectByPrimaryKey(id);
    }

    public StrategyDetail getByDetailId(Long id) {
        return strategyDetailMapper.selectByPrimaryKey(id);
    }
}
